package br.unicentro.lynx.model.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import br.unicentro.lynx.model.factory.ConnectionFactory;
import br.unicentro.lynx.model.pojo.Fine;

public class FineDao {
	private String sql;
	private Connection connection;
	private PreparedStatement statement;
	private ResultSet result;

	public void insert(Fine fine) {
		open();
		this.sql = "INSERT INTO Fines (prince_fine) VALUES (?)";
		try {
			this.statement = connection.prepareStatement(sql);
			this.statement.setDouble(1, fine.getPrinceFine());
			this.statement.execute();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			close();
		}
	}

	public void update(Fine fine) {
		open();
		this.sql = "UPDATE Fines SET prince_fine = ? WHERE id_fine = ?";
		try {
			this.statement = connection.prepareStatement(sql);
			this.statement.setDouble(1, fine.getPrinceFine());
			this.statement.setInt(2, fine.getIdFine());
			this.statement.execute();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			close();
		}
	}

	public void delete(Fine fine) {
		open();
		this.sql = "DELETE FROM Fines WHERE id_fine = ?";
		try {
			this.statement = connection.prepareStatement(sql);
			this.statement.setInt(1, fine.getIdFine());
			this.statement.execute();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			close();
		}
	}

	public Fine search(Integer id_fine) {
		open();
		this.sql = "SELECT * FROM Fines WHERE id_fine = ?";
		try {
			this.statement = connection.prepareStatement(sql);
			this.statement.setInt(1, id_fine);
			this.result = this.statement.executeQuery();
			if (this.result.next()) {
				return new Fine(result.getInt("id_fine"),
						result.getDouble("prince_fine"));
			} else {
				return null;
			}
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			close();
		}
	}

	public ArrayList<Fine> list() {
		open();
		this.sql = "SELECT * FROM Fines";
		ArrayList<Fine> fines = new ArrayList<Fine>();
		try {
			this.statement = connection.prepareStatement(sql);
			this.result = this.statement.executeQuery();
			while (this.result.next()) {
				fines.add(new Fine(result.getInt("id_fine"), result
						.getDouble("prince_fine")));
			}
			return fines;
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			try {
				this.result.close();
				close();
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
		}
	}

	private void open() {
		this.connection = ConnectionFactory.getConnection(
				ConnectionFactory.URL, ConnectionFactory.USER,
				ConnectionFactory.PASSWORD);
	}

	private void close() {
		try {
			this.statement.close();
			this.connection.close();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
}
